<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<%@ page import="com.example.dao.UserDao" %>
<%@ page import="com.example.domain.User" %>
<%@ page import="com.example.util.DBUtil" %>
<%@ page import="java.util.Optional" %>
<%
    // 检查是否为GET请求
    if (!"GET".equalsIgnoreCase(request.getMethod())) {
        response.sendError(HttpServletResponse.SC_METHOD_NOT_ALLOWED);
        return;
    }

    request.setCharacterEncoding("UTF-8");
    
    try {
        int userId = Integer.parseInt(request.getParameter("id"));
        
        UserDao userDao = new UserDao();
        Optional<User> userOptional = userDao.getUserById(userId);
        
        if (userOptional.isPresent()) {
            User user = userOptional.get();
            request.setAttribute("user", user);
            request.setAttribute("userId", userId);
        } else {
            response.sendRedirect("user_list.jsp?error=用户不存在");
            return;
        }
    } catch (NumberFormatException e) {
        response.sendRedirect("user_list.jsp?error=无效的用户ID");
        return;
    } catch (Exception e) {
        response.sendRedirect("user_list.jsp?error=加载用户信息失败: " + e.getMessage());
        return;
    }
%>
<!DOCTYPE html>
<html>
<head>
    <meta charset="UTF-8">
    <title>编辑用户信息</title>
    <link rel="stylesheet" href="../assets/css/styles.css">
    <link rel="stylesheet" href="../assets/css/menu.css">
    <style>
        body {
            font-family: Arial, sans-serif;
            margin: 0;
            padding: 20px;
            background-color: #ffffff;
        }
        .container {
            max-width: 800px;
            margin: 0 auto;
            background-color: white;
            padding: 20px;
            border-radius: 5px;
            box-shadow: 0 0 10px rgba(0,0,0,0.1);
        }
        .header {
            display: flex;
            justify-content: space-between;
            align-items: center;
            padding-bottom: 20px;
            border-bottom: 1px solid #eee;
            margin-bottom: 20px;
        }
        .btn {
            display: inline-block;
            padding: 8px 16px;
            background-color: #ffffff;
            color: #000000;
            text-decoration: none;
            border-radius: 4px;
            border: 1px solid #ddd;
            cursor: pointer;
        }
        .btn:hover {
            background-color: #000000;
            color: #ffffff;
        }
        .btn-danger {
            background-color: #ffffff;
            color: #000000;
            border: 1px solid #000000;
        }
        .btn-danger:hover {
            background-color: #000000;
            color: #ffffff;
        }
        .btn-success {
            background-color: #ffffff;
            color: #000000;
            border: 1px solid #000000;
        }
        .btn-success:hover {
            background-color: #000000;
            color: #ffffff;
        }
        .form-group {
            margin-bottom: 15px;
        }
        .form-group label {
            display: block;
            margin-bottom: 5px;
            font-weight: bold;
        }
        .form-group input, .form-group textarea, .form-group select {
            width: 100%;
            padding: 8px;
            border: 1px solid #ddd;
            border-radius: 4px;
            box-sizing: border-box;
        }
    </style>
</head>
<body>
<div class="container">
    <div class="header">
        <h1>编辑用户信息</h1>
        <a href="user_list.jsp" class="btn">返回用户列表</a>
    </div>
    
    <% if (request.getParameter("error") != null) { %>
        <div style="color: red; margin-bottom: 15px;">
            <%= request.getParameter("error") %>
        </div>
    <% } %>
    
    <form action="user_edit_do.jsp" method="post">
        <input type="hidden" name="id" value="<%= ((User)request.getAttribute("user")).getId() %>">
        
        <div class="form-group">
            <label for="username">用户名:</label>
            <input type="text" id="username" name="username" value="<%= ((User)request.getAttribute("user")).getUsername() %>" required>
        </div>
        
        <div class="form-group">
            <label for="realname">真实姓名:</label>
            <input type="text" id="realname" name="realname" value="<%= ((User)request.getAttribute("user")).getRealname() != null ? ((User)request.getAttribute("user")).getRealname() : "" %>">
        </div>
        
        <div class="form-group">
            <label for="password">密码:</label>
            <input type="password" id="password" name="password" placeholder="留空表示不修改密码">
        </div>
        
        <div class="form-group">
            <label for="email">邮箱:</label>
            <input type="email" id="email" name="email" value="<%= ((User)request.getAttribute("user")).getEmail() != null ? ((User)request.getAttribute("user")).getEmail() : "" %>" required>
        </div>
        
        <div class="form-group">
            <label for="phone">电话:</label>
            <input type="text" id="phone" name="phone" value="<%= ((User)request.getAttribute("user")).getPhone() != null ? ((User)request.getAttribute("user")).getPhone() : "" %>">
        </div>
        
        <div class="form-group">
            <input type="submit" value="更新用户" class="btn btn-success">
            <input type="reset" value="重置" class="btn">
            <button type="button" class="btn" onclick="history.back()">取消</button>
        </div>
    </form>
</div>
</body>
</html>